/**
 * 基于链表实现的栈
 *
 * @author Kevin
 * @date 2018-11-24
 */
public class LinkedListStack<E> implements Stack<E> {

    /**
     * 列表
     */
    private LinkedList<E> list;

    public LinkedListStack() {
        this.list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return this.list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    @Override
    public void push(E e) {
        this.list.addFirst(e);
    }

    @Override
    public E pop() {
        return this.list.removeFirst();
    }

    @Override
    public E peek() {
        return this.list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Stack: top ");
        sb.append(this.list);
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedListStack<Integer> stack = new LinkedListStack<>();

        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);
    }

}
